/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    internal partial class ActionEditor
    {

        private bool _isPlaying;

        /// <summary>
        /// 播放
        /// </summary>
        public bool IsPlaying
        {
            get => _isPlaying;
            set
            {
                _isPlaying = value;
                if (_isPlaying)
                {
                    startTime = DateTime.Now;
                    startFrameIndex = _currentSelectFrameIndex;
                    // OnPlay
                    for (int i = 0; i < trackList.Count; i++)
                    {
                        trackList[i].OnPlay(_currentSelectFrameIndex);
                    }

                    if (!IsRecordEvent)
                    {
                        DisableElement(TranscribeButton);
                    }
                }
                else
                {
                    EnableElement(TranscribeButton);
                    // OnStop
                    for (int i = 0; i < trackList.Count; i++)
                    {
                        trackList[i].OnStop();
                    }
                }
            }
        }


        private bool _isRecordEvent = false;

        /// <summary>
        /// 录制
        /// </summary>
        public bool IsRecordEvent
        {
            get { return _isRecordEvent; }
            set
            {
                _isRecordEvent = value;
                if (_isRecordEvent)
                {
                    if (recordOffImg == null)
                    {
                        var imagePath = ActionEditorUtility.GetImageRootPath();
                        recordOffImg =
                            AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath + "TrackRecordButtonEnabled@2x.png");
                    }

                    TranscribeButton.style.backgroundImage = new StyleBackground(recordOffImg);
                    CurrentSelectFrameIndex = 0;
                    if (!_isPlaying)
                    {
                        IsPlaying = true;
                    }

                    DisablePlayButtons();
                }
                else
                {
                    if (recordOnImg == null)
                    {
                        var imagePath = ActionEditorUtility.GetImageRootPath();
                        recordOnImg =
                            AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath + "d_TrackRecordButtonEnabled@2x.png");
                    }

                    TranscribeButton.style.backgroundImage = new StyleBackground(recordOnImg);
                    if (_isPlaying)
                    {
                        IsPlaying = false;
                    }

                    CurrentSelectFrameIndex = 0;
                    EnablePlayButtons();
                }
            }
        }

        private DateTime startTime;
        private int startFrameIndex;

        /// <summary>
        /// 驱动时间轴
        /// </summary>
        private void Update()
        {
            if (IsPlaying)
            {
                // 得到时间差
                float time = (float)DateTime.Now.Subtract(startTime).TotalSeconds;

                // 确定时间轴的帧率
                float frameRote;
                if (_actionData != null) frameRote = _actionData.FrameRote;
                else frameRote = _actionEditorConfig.defaultFrameRote;

                // 根据时间差计算当前的选中帧
                CurrentSelectFrameIndex = (int)((time * frameRote) + startFrameIndex);
                // 到达最后一帧自动暂停
                if (CurrentSelectFrameIndex == CurrentFrameCount)
                {
                    IsPlaying = false;
                }
            }
        }

        private void TickPreView()
        {
            // 驱动技能表现
            if (_actionData != null && PreviewCharacterObj != null)
            {
                for (int i = 0; i < trackList.Count; i++)
                {
                    trackList[i].TickPreView(_currentSelectFrameIndex);
                }
            }
        }
    }
}
